# // ***************************************************************************
# // 
# // Copyright (c) Scriptimus Ex Machina.  All rights reserved.
# // 
# // Microsoft Deployment Toolkit Support Script
# //
# // File:      ZTICreateFolderandShare.ps1
# // 
# // Version:   1.00 
# // 
# // Purpose:   Creates a folder and sets its permissions
# // 
# // ***************************************************************************

<#
.Synopsis
   Creates a folder and sets its permissions
.DESCRIPTION
   The ZTICreateFolderandShare creates a folder and sets complex permissions.
.EXAMPLE
    ZTICreateFolderandShare -Path "C:\CRLDist" -Share "CRLDist$" -User "$TSEnv:DomainNETBIOSName\$TSEnv:DC1OSDComputerName$"

    This example will create a folder called C:\CRLDist and share it as CRLDist$ then apply the permisions from a task sequence variable in MDT

.NOTES
   Author: Andrew Barnes
   Version: 1.0
   Created: 4 September 2012
   Last updated: 22 November 2012
   Website: http:\\scriptimus.wordpress.com

#>

Param(

    [String]$Path = "C:\CRLDist", 

    [String]$Share = "CRLDist$",

    [string]$User = ((Get-WmiObject Win32_NTDomain)[1].DomainName)+"\"+((Get-WmiObject Win32_NTDomain)[1].DomainControllerName).substring(2)+"$"

)

Begin{

    Write-Host "Begining script"
    
    }

Process{

    Write-Host "Processing Script"

    # Create Folder

    if (!(Test-Path -Path $Path)) 


        {

        Write-Host "Creating Folder: $Path"

        New-Item -Path $Path -Type Directory 

        }

    # Add File Permissions

    Write-Host "Add File Permission to: $Path"

    $acl = Get-Acl $Path

    $acl.SetAccessRuleProtection($False, $False)

    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($User,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")

    $acl.AddAccessRule($rule)

    Set-Acl $Path $acl

    # Create Share

    Write-Host "Sharing folder: $Path as share: $Share"

    Net Share $Share=$Path "/Grant:Everyone,Full"
    
        }

End{

    Write-Host "End Script"

    }
